package com.android.view.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.presenter.info.ResetAccountPasswordPresenter;
import com.android.presenter.iview.IResetAccountPasswordView;
import com.android.util.ToastUtil;
import com.android.view.activity.base.Constants;
import com.android.view.activity.base.ToolbarBaseActivity;
import com.android.R;

import butterknife.BindView;
import retrofit2.Response;

/**
 * 重设密码，输入两次
 * Created by TenFace on 2018/4/23.
 */

public class SetNewPasswordActivity extends ToolbarBaseActivity implements IResetAccountPasswordView {
    private static final String TAG = RegisterActivity.class.getSimpleName();
    @BindView(R.id.etNewPWD)
    EditText etNewPWD;
    @BindView(R.id.etNewPWDAgain)
    EditText etNewPWDAgain;
    @BindView(R.id.btnSubmitNewPWD)
    Button btnSubmitNewPWD;
    private Context context;
    private static String intentKey = "TOKEN";
    private String token;
    private ResetAccountPasswordPresenter resetAccountPasswordPresenter;

    public static void actionStart(Context context, String token) {
        Intent intent = new Intent(context, SetNewPasswordActivity.class);
        intent.putExtra(intentKey, token);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constants.activityList.put(TAG, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Constants.activityList.remove(TAG);
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_set_new_password);
    }

    @Override
    protected void initView() {
        context = this;
        int color = getResourceColor(R.color.white);
        setStatusBarColor(color, 0);
        setStatusBarMode(true);
        Intent intent = getIntent();
        token = intent.getStringExtra(intentKey);
    }

    @Override
    protected void obtainData() {
        resetAccountPasswordPresenter = new ResetAccountPasswordPresenter(this);
    }

    @Override
    protected void initEvent() {
        btnSubmitNewPWD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etNewPWD.getText().length() < 8) {
                    ToastUtil.show("请输入8位以上密码！");
                } else if (etNewPWDAgain.getText().length() < 8) {
                    ToastUtil.show("请输入8位以上密码！");
                } else if (!etNewPWD.getText().toString().equals(etNewPWDAgain.getText().toString())) {
                    ToastUtil.show("两次密码输入不一致，请重试！");
                } else {
                    resetAccountPasswordPresenter.resetAccountPassword(etNewPWD.getText().toString(), token);
                }
            }
        });
    }

    @Override
    public void resetPasswordSuccess(Response<Void> voidResponse) {
        if (voidResponse.code() == 200) {
            ToastUtil.show("重设密码成功！");
            Constants.activityList.get("SecurityVerificationActivity").finish();
            Constants.activityList.get("ResetPasswordActivity").finish();
            finish();
        }
    }

    @Override
    public void resetPasswordFail(int status, String desc) {
        ToastUtil.show("请求失败，请稍后重试！");
    }
}
